package com.aiwei.tdjk.utils;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件操作，对commons.io的再封装
 * Created by dell on 2016/8/1.
 */
public class FileUtilsTool {
    /**
     * 获取根路径
     * @return path 获取的根路径
     */
    public static String getRootPath()
    {   String path = "";
        if (path.equals(""))
        {
            try {
                path = new File(FileUtilsTool.class.getClassLoader().getResource("").toURI()).getPath();
//                path = path.replace("WEB-INF\\classes", "");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return path;
    }

    /**
     * 当前程序的相对路径
     * @param pathStr 当前程序路径
     * @return 当前程序路径的绝对路径
     */
    public static String getRelationPath(String pathStr){
        return getRootPath() + "/" +pathStr;
    }

    /**
     * Cleans a directory without deleting it.
     * @param directory directory to clean
     */
    public static void cleanDirectory(File directory){
        try {
            FileUtils.cleanDirectory(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Cleans a directory without deleting it.
     * @param directory directory to clean
     */
    public static void cleanDirectory(String directory){
        cleanDirectory(getFile(directory));
    }

    /**
     * Compares the contents of two files to determine if they are equal or not.
     * @param file1 the first file
     * @param file2 the second file
     * @return true if the content of the files are equal or they both don't exist, false otherwise
     */
    public static boolean contentEquals(File file1,File file2){
        boolean isEquals = false;
        try {
            isEquals = FileUtils.contentEquals(file1,file2);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return isEquals;
    }

    /**
     * Compares the contents of two files to determine if they are equal or not.
     * @param file1 the first file
     * @param file2 the second file
     * @return true if the content of the files are equal or they both don't exist, false otherwise
     */
    public static boolean contentEquals(String file1,String file2){
        return contentEquals(getFile(file1),getFile(file2));
    }

    /**
     * copies a whole directory to a new location preserving the file dates.
     * @param srcDir an existing directory to copy, must not be null
     * @param destDir the new directory, must not be null
     */
    public static void copyDirectory(File srcDir,File destDir){
        try {
            FileUtils.copyDirectory(srcDir,destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * copies a whole directory to a new location preserving the file dates.
     * @param srcDir an existing directory to copy, must not be null
     * @param destDir the new directory, must not be null
     */
    public static void copyDirectory(String srcDir,String destDir){
        copyDirectory(getFile(srcDir),getFile(destDir));
    }

    /**
     * Copies a directory to within another directory preserving the file dates.
     * @param srcDir the srcDir
     * @param destDir the destDir
     */
    public static void copyDirectoryToDirectory(File srcDir, File destDir){
        try {
            FileUtils.copyDirectoryToDirectory(srcDir,destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Copies a directory to within another directory preserving the file dates.
     * @param srcDir
     * @param destDir
     */
    public static void copyDirectoryToDirectory(String srcDir, String destDir){
        copyDirectoryToDirectory(getFile(srcDir),getFile(destDir));
    }

    /**
     * Copies a file to a new location preserving the file date.
     * @param srcFile
     * @param destFile
     */
    public static void copyFile(File srcFile, File destFile){
        try {
            FileUtils.copyFile(srcFile,destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Copies a file to a new location preserving the file date.
     * @param srcFile
     * @param destFile
     */
    public static void copyFile(String srcFile, String destFile){
        copyFile(getFile(srcFile),getFile(destFile));
    }

    /**
     * Copies a file to a directory preserving the file date.
     * @param srcFile
     * @param destDir
     */
    public static void copyFileToDirectory(File srcFile, File destDir){
        try {
            FileUtils.copyFileToDirectory(srcFile,destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Copies a file to a directory preserving the file date.
     * @param srcFile
     * @param destDir
     */
    public static void copyFileToDirectory(String srcFile, String destDir) {
        copyFileToDirectory(getFile(srcFile), getFile(destDir));
    }

    /**
     * Copies bytes from an InputStream source to a file destination.
     * @param source
     * @param destination
     */
    public static void copyInputStreamToFile(InputStream source, File destination){
        try {
            FileUtils.copyInputStreamToFile(source,destination);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Copies bytes from an InputStream source to a file destination.
     * @param source
     * @param destination
     */
    public static void copyInputStreamToFile(InputStream source, String destination){
       copyInputStreamToFile(source,getFile(destination));
    }

    /**
     * Deletes a directory recursively.
     * @param directory
     */
    public static void 	deleteDirectory(File directory){
        try {
            FileUtils.deleteDirectory(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Deletes a directory recursively.
     * @param directory
     */
    public static void 	deleteDirectory(String directory){
       deleteDirectory(getFile(directory));
    }

    /**
     * Deletes a file, never throwing an exception.
     * @param file 要删除的文件
     */
    public static void deleteQuietly(File file){
        FileUtils.deleteQuietly(file);
    }

    /**
     * Deletes a file, never throwing an exception.
     * @param file 要删除的文件名
     */
    public static void deleteQuietly(String file){
        deleteQuietly(getFile(file));
    }

    /**
     * deletes a file.
     * @param file
     */
    public static void forceDelete(File file){
        try {
            FileUtils.forceDelete(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void forceDelete(String filePath){
        forceDelete(getFile(filePath));
    }

    /**
     * Makes a directory, including any necessary but nonexistent parent directories.
     * @param directory
     */
    public static void forceMkdir(File directory){
        try {
            FileUtils.forceMkdir(directory);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Makes a directory, including any necessary but nonexistent parent directories.
     * @param directory
     */
    public static void forceMkdir(String directory){
        forceMkdir(getFile(directory));
    }

    /**
     * Returns a File representing the user's home directory.
     * @return
     */
    public static File getUserDirectory(){
        return FileUtils.getUserDirectory();
    }

    /**
     * Returns the path to the user's home directory.
     * @return
     */
    public static String getUserDirectoryPath(){
        return FileUtils.getUserDirectoryPath();
    }

    /**
     * Moves a directory.
     * @param srcDir
     * @param destDir
     */
    public static void 	moveDirectory(File srcDir, File destDir){
        try {
            FileUtils.moveDirectory(srcDir, destDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * Moves a directory.
     * @param srcDir
     * @param destDir
     */
    public static void 	moveDirectory(String srcDir, String destDir){
        moveDirectory(getFile(srcDir),getFile(destDir));
    }

    /**
     * Moves a directory to another directory.
     * @param src
     * @param destDir
     * @param createDestDir
     */
    public static void moveDirectoryToDirectory(File src, File destDir, boolean createDestDir){
        try {
            FileUtils.moveDirectoryToDirectory(src, destDir, createDestDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Moves a directory to another directory.
     * @param src
     * @param destDir
     * @param createDestDir
     */
    public static void moveDirectoryToDirectory(String src, String destDir, boolean createDestDir){
        moveDirectoryToDirectory(getFile(src),getFile(destDir),createDestDir);
    }

    /**
     * Moves a directory to another directory.
     * @param src
     * @param destDir
     */
    public static void moveDirectoryToDirectory(String src, String destDir){
        moveDirectoryToDirectory(src, destDir,true);
    }


    /**
     * Moves a file.
     * @param srcFile
     * @param destFile
     */
    public static void 	moveFile(File srcFile, File destFile){
        try {
            FileUtils.moveFile(srcFile, destFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Moves a file.
     * @param srcFile
     * @param destFile
     */
    public static void 	moveFile(String srcFile, String destFile){
        moveFile(getFile(srcFile),getFile(destFile));
    }

    /**
     * Moves a file to a directory.
     * @param srcFile
     * @param destDir
     * @param createDestDir
     */
    public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir){
        try {
            FileUtils.moveFileToDirectory(srcFile, destDir, createDestDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Movies a file to a directory.
     * @param srcFile
     * @param destDir
     * @param createDestDir
     */
    public static void moveFileToDirectory(String srcFile, String destDir, boolean createDestDir){
        moveFileToDirectory(getFile(srcFile), getFile(destDir), createDestDir);
    }

    /**
     * Moves a file to a directory.
     * @param srcFile
     * @param destDir
     */
    public static void moveFileToDirectory(File srcFile, File destDir){
        moveFileToDirectory(srcFile, destDir, true);
    }

    /**
     *  Moves a file to a directory.
     * @param srcFile
     * @param destDir
     */
    public static void moveFileToDirectory(String srcFile, String destDir){
        moveFileToDirectory(srcFile, destDir, true);
    }

    /**
     * Moves a file or directory to the destination directory.
     * @param src
     * @param destDir
     * @param createDestDir
     */
    public static void moveToDirectory(File src, File destDir, boolean createDestDir){
        try {
            FileUtils.moveToDirectory(src, destDir, createDestDir);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  Moves a file or directory to the destination directory.
     * @param src
     * @param destDir
     * @param createDestDir
     */
    public static void moveToDirectory(String src, String destDir, boolean createDestDir){
        moveToDirectory(getFile(src), getFile(destDir), createDestDir);
    }

    /**
     * Returns the size of the specified file or directory.
     * @param file
     * @return
     */
    public static long sizeOf(File file){
        return FileUtils.sizeOf(file);
    }

    /**
     * Returns the size of the specified file or directory.
     * @param filePath
     * @return
     */
    public static long sizeOf(String filePath){
        return sizeOf(getFile(filePath));
    }

    /**
     * Counts the size of a directory recursively (sum of the length of all files).
     * @param directory
     * @return
     */
    public static long sizeOfDirectory(File directory){
        return FileUtils.sizeOfDirectory(directory);
    }

    /**
     * Counts the size of a directory recursively (sum of the length of all files).
     * @param directory
     * @return
     */
    public static long sizeOfDirectory(String directory){
        return sizeOfDirectory(getFile(directory));
    }

    /**
     * Implements the same behaviour as the "touch" utility on Unix.
     * @param file
     */
    public static void touch(File file){
        try {
            FileUtils.touch(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Implements the same behaviour as the "touch" utility on Unix.
     * @param filePath
     */
    public static void touch(String filePath){
        touch(getFile(filePath));
    }

    /**
     * Writes a byte array to a file creating the file if it does not exist.
     * @param file the file to write
     * @param data the content to write to the file
     */
    public static void writeByteArrayToFile(File file, byte[] data){
        try {
            FileUtils.writeByteArrayToFile(file,data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Writes a byte array to a file creating the file if it does not exist.
     * @param file the file to write
     * @param data the content to write to the file
     */
    public static void writeByteArrayToFile(String file, byte[] data){
        writeByteArrayToFile(getFile(file),data);
    }


    /**
     * Writes a String to a file creating the file if it does not exist.
     * @param file the file to write
     * @param data the content to write to the file
     * @param encoding the content encoding
     */
    public static void 	writeStringToFile(File file, String data, String encoding){
        try {
            FileUtils.writeStringToFile(file, data,encoding);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Writes a String to a file creating the file if it does not exist.
     * @param file
     * @param data
     * @param encoding
     */
    public static void 	writeStringToFile(String file, String data, String encoding){
       writeStringToFile(getFile(file), data,encoding);
    }

    /**
     *
     * @param file
     * @param data
     */
    public static void 	writeStringToFile(File file, String data){
        try {
            FileUtils.writeStringToFile(file, data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * renames fileName to
     * @param srcFile 需要重命名的文件
     * @param destFile 重命名后的文件
     */
    public static void renameTo(File srcFile,File destFile){
        copyFile(srcFile,destFile);
        deleteQuietly(srcFile);
    }

    /**
     * renames fileName to
     * @param srcFile 源文件
     * @param destFile 目标文件
     */
    public static void renameTo(String srcFile,String destFile){
        renameTo(getFile(srcFile),getFile(destFile));
    }

    /**
     * 修改文件夹下所有文件后缀名
     * @param srcDirectory 目标文件夹
     * @param newFilesSuffixName 新后缀名
     */
    public static void modifyDirectoryFileSuffixTo(File srcDirectory,String newFilesSuffixName){
        for (File file : srcDirectory.listFiles()) {
            if (file.isDirectory()){
                modifyDirectoryFileSuffixTo(file,newFilesSuffixName);
            }else {
                modifyFileSuffixTo(file,newFilesSuffixName);
            }
        }
    }
    /**
     * 修改文件夹下所有文件后缀名
     * @param srcDirectory 目标文件夹
     * @param newFilesSuffixName 新后缀名
     */
    public static void modifyDirectoryFileSuffixTo(String srcDirectory,String newFilesSuffixName){
        modifyDirectoryFileSuffixTo(getFile(srcDirectory),newFilesSuffixName);
    }

    /**
     * 修改文件后缀名
     * @param file 要修改文件后缀名的文件
     * @param newFilesSuffixName 后缀名
     */
    public static void modifyFileSuffixTo(File file,String newFilesSuffixName){
        String fileName = file.getName();
        String newFileName = fileName;
        int index = fileName.lastIndexOf(".");
        if (index > 0){//存在后缀名
            String suffixName = fileName.substring(index + 1).toUpperCase();
            if (!suffixName.equals(newFilesSuffixName.toUpperCase())){//与新后缀名不相同
                newFileName = fileName.substring(0,index)+ "." + newFilesSuffixName;
                renameTo(file,getFile(file.getParent()+"//"+newFileName));
            }
        }else {
            newFileName = newFileName + "." + newFilesSuffixName;
            renameTo(file,getFile(file.getParent()+"//"+newFileName));
        }
    }

    /**
     *
     * @param file
     * @return
     * @throws Exception
     */
    public static String readFileToString(File file) throws Exception{
        return FileUtils.readFileToString(file);
    }


    public static String readFileToString(String filePath) throws Exception{
        return readFileToString(getFile(filePath));
    }


    public static String readFileToString(File file,String encoding) throws Exception{
        return FileUtils.readFileToString(file,encoding);
    }

    public static String readFileToString(String filePath,String encoding) throws Exception{
        return readFileToString(getFile(filePath),encoding);
    }


    /**
     * 把路径转换为File对象
     * @param path 文件路径
     * @return File对象
     */
    public static File getFile(String path){
        File file = null;
        if (StringUtils.isNotNullOrEmpty(path)){
            file = new File(path);
        }
        return file;
    }

    public static void main(String[] args) {
        String str = getUserDirectoryPath();
        System.out.println(str);
    }


}
